package mylibrary.security.UDPChat;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class InitDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2750765028820121861L;

	private static final int DEFAULT_WIDTH = 200;
	private static final int DEFAULT_HEIGHT = 210;
	private int receivePort;
	private int sendPort;
	private String userName;
	private String remoteHost;
	private String localHost;
	private boolean cancelled = true;
	public int getReceivePort() {
		return receivePort;
	}
	public int getSendPort() {
		return sendPort;
	}
	public String getUserName() {
		return userName;
	}
	public String getRemoteHost() {
		return remoteHost;
	}
	public String getLocalHost() {
		return localHost;
	}
	public boolean isCancelled() {
		return cancelled;
	}
	
	public InitDialog(Frame owner) {
		super(owner, "Initializing Dialog", true);
		String local;
		try {
			local = InetAddress.getLocalHost().getHostAddress();
		} catch (Exception e) {
			local = "localhost";
		}
		
		final JTextField remoteHostField = new JTextField(local, 10);
		final JTextField localHostField = new JTextField(local, 10);
		final JTextField receivePortField = new JTextField("8001", 10);
		final JTextField sendPortField = new JTextField("8002", 10);
		final JTextField usernameField = new JTextField("zlex", 10);
		
		JPanel inputPanel = new JPanel();
		inputPanel.setMinimumSize(new Dimension(80, 120));
		inputPanel.setBorder(BorderFactory.createEtchedBorder());
		inputPanel.add(new JLabel("Destination Host:"));
		inputPanel.add(remoteHostField);
		inputPanel.add(new JLabel("Local Host:"));
		inputPanel.add(localHostField);
		inputPanel.add(new JLabel("Receive Port:"));
		inputPanel.add(receivePortField);
		inputPanel.add(new JLabel("Send Port:"));
		inputPanel.add(sendPortField);
		inputPanel.add(new JLabel("User nickname:"));
		inputPanel.add(usernameField);
		
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				remoteHost = remoteHostField.getText();
				localHost = localHostField.getText();
				receivePort = Integer.parseInt(receivePortField.getText());
				sendPort = Integer.parseInt(sendPortField.getText());
				userName = usernameField.getText();
				cancelled = false;
				InitDialog.this.dispose();
			}
		});
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				InitDialog.this.dispose();
			}
		});
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		this.getContentPane().add(inputPanel, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		
		this.setMinimumSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}
}
